//
//  OEShopCarViewController.m
//  OneEightyNine
//
//  Created by apple on 2019/3/7.
//  Copyright © 2019 stoneobs. All rights reserved.
//

#import "OEShopCarViewController.h"
#import "OEShopGoodTableViewCell.h"
#import "OECommitViewController.h"
@interface OEShopCarViewController ()
@property(nonatomic, strong) NSMutableArray<OEGoodModel*>                     *dataSouce;/**< 数据源 */
@end

@implementation OEShopCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"shop Car";
    
    self.tableView.frame = CGRectMake(0, TMUtils.navgationBarBootom, UIScreenWidth, TMUtils.tabBarTop - TMUtils.navgationBarBootom);
    self.tableView.contentInset = UIEdgeInsetsZero;
    
    __weak typeof(self) weakSelf =  self;
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, self.tableView.tableHeaderView.height + 30, UIScreenWidth, 400) title:@"NO More Data" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf sendNetRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    

    
    STButton * button = [[STButton alloc] initWithFrame:CGRectMake(30, TMUtils.tabBarTop, UIScreenWidth - 60, 50)
                                                  title:@"GO  Down"
                                             titleColor:UIColor.whiteColor
                                              titleFont:15
                                           cornerRadius:25
                                        backgroundColor:OE_LIMITE_COLOR
                                        backgroundImage:nil
                                                  image:nil];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    button.centerX = self.tableView.width / 2;
    [button setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedConfimButton];
    }];
    [self.view addSubview:button];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self sendNetRequest];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return OEShopGoodTableViewCell.cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    OEShopGoodTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[OEShopGoodTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    OEGoodModel * model = self.dataSouce[indexPath.row];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    __weak typeof(self) weakSelf =  self;
    [cell.addButton setClicAction:^(UIButton *sender) {
        NSString * number = @(model.subtotal_number.integerValue + 1).description;
        [OEHomeDataController addGoodToCar:model.goods_id handle:^(BOOL success, NSString *errorString, NSDictionary *resp) {
            if (success) {
                model.subtotal_number = number;
                [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }
        }];
    }];
    [cell.reduceButton setClicAction:^(UIButton *sender) {
        if (model.subtotal_number.integerValue) {
            NSString * number = @(model.subtotal_number.integerValue - 1).description;
            NSString * realnumber = @"-1";
            [OEHomeDataController sendReduceGoodToCar:model.goods_id number:realnumber handle:^(BOOL success, NSString *errorString, NSDictionary *resp) {
                if (success) {
                    model.subtotal_number = number;
                    [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
                }
            }];
        }else{
            [SVProgressHUD showInfoWithStatus:@"You can't pull any less"];
        }
    }];
    return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    __weak typeof(self) weakSelf =  self;
    UITableViewRowAction * rowActiondelete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        OEGoodModel * model = weakSelf.dataSouce[indexPath.row];
        [OEHomeDataController sendDeleteShopCarRequestWithRec_id:model.rec_id  handle:^(BOOL success, NSString *errorString, NSDictionary *resp) {
            if (success) {
                [weakSelf.tableView.mj_header beginRefreshing];
            }
        }];
        
    }];
    rowActiondelete.backgroundColor = UIColor.redColor;
    return @[rowActiondelete];
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OEGoodModel * model = self.dataSouce[indexPath.row];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)onSelctedConfimButton{
    if (!self.dataSouce.count) {
        [self st_showAlertTitle:@"The shopping cart was empty" message:@"" andWithBlock:^(NSString *name) {
            
        }];
        return;
    }
    OECommitViewController * vc = OECommitViewController.new;
    vc.dataSouce = self.dataSouce       ;
    vc.model = self.model;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark --NetWork Method
- (void)sendNetRequest{
    [OEHomeDataController sendFetchShopCarListRequestWithSupplier_id:self.supplier_id handle:^(NSString * _Nonnull cart_number, NSString * _Nonnull goods_price, NSArray * _Nonnull goods) {
        
        self.dataSouce = goods.mutableCopy;
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    }];
}

@end
